﻿using UnityEngine;

namespace Core
{
    /// <summary>
    /// 根据间隔创建子弹buff
    /// </summary>
    [Buff]
    public class CreateIntervalBulletBuff : BuffSystem
    {
        private int bulletId;
        private float interval;
        private float countdown;
        private Transform drawRange;
        protected override void OnAwake()
        {
            int.TryParse(config.param1, out bulletId);
            float.TryParse(config.param2, out interval);
            countdown = interval;
#if UNITY_EDITOR
            var prefab = World.GetComponent<ResourceComponent>().Load<GameObject>("prefabs/drawRange");
            if (prefab != null)
            {
                drawRange = GameObject.Instantiate(prefab).transform;
                drawRange.transform.position = target.Position;
                drawRange.GetComponent<DrawRange>().radius = 1.5f;
            }
#endif
        }

        protected override void OnUpdate()
        {
            countdown -= Time.deltaTime;
            if (countdown <= 0)
            {
                var bullet = World.GetComponent<BulletComponent>().CreateBullet(target, bulletId);
                bullet.Launch(target.transform.position, target.transform.forward);
                countdown = interval;
            }
        }
    }

}